﻿package com.fluidsoftwarellc.xon 
{
	/**
	 * ...
	 * @author Scott Tompkins
	 * FXON (Flash XML Object Notation) and Parser (http://code.google.com/p/as3-fxon/)
	 * Copyright (C) 2011  Scott C. Tompkins
	 * 
	 * This program is free software: you can redistribute it and/or modify
	 * it under the terms of the GNU General Public License as published by
	 * the Free Software Foundation, either version 3 of the License, or
	 * (at your option) any later version.
	 * 
	 * This program is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 * GNU General Public License for more details.
	 * 
	 * You should have received a copy of the GNU General Public License
	 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */
	import flash.utils.*;
	
	public class XONParser
	{

		public function XONParser() {

			
		}
		
		
		
		public static function parseXONObject(xonObject:XML, basePackage:String = ""):* 
		{
			//Check to see if this xonObject has a basePackage attribute, if so use it... if not, use the one that was passed in.
			var xonBasePackage = xonObject.attribute("basePackage");
			if (new String(xonBasePackage) != "") {
				basePackage = xonBasePackage;
			}
			
			//Instantiate the xonObject's type
			var classPath:String = xonObject.attribute("type");
			if (basePackage != "" && classPath.charAt() == ".") {
				classPath = basePackage + classPath;
			}
			if (classPath == "") {
				classPath = "Object";
			}
			var classRef:Class = getDefinitionByName(classPath) as Class;
			var object:* = new classRef();			
			
			//Propogate xonObject Members
			for each (var xonMember:XML in xonObject.children()) {
				object[xonMember.attribute("name")] = parseXONMember(xonMember, basePackage);
			}
			
			return object;
		}
		
		public static function parseXONMember(xonMember:XML, basePackage:String = ""):* 
		{
			var member:*;
			switch(xonMember.localName())
			{
				case "Object":
					//object recursion
					member = parseXONObject(xonMember, basePackage);
					break;
				case "Number":
					member = parseFloat(xonMember);
					break;
				case "String":
					member = new String(xonMember);
					break;
				case "Boolean":
					member = cStringBool(xonMember);
					break;
				case "Date":
					member = new Date(Date.parse(xonMember));
					break;
				case "Array":
					member = new Array();
					for each (var xonArrayMember:XML in xonMember.children()) {
						//array recursion
						member.push(parseXONMember(xonArrayMember, basePackage));
					}
					break;
					
				default:
					trace("Unaccounted XML Node Found: " + xonMember.localName());
			}

			return member;
		}
		
		
		
		private static function cStringBool(input:String):Boolean
		{
			var bReturn:Boolean = false;
			switch (input.toLowerCase()) 
			{
				case "1":
					bReturn = true;
					break;
				case "true":
					bReturn = true;
					break;
			}
			return bReturn;
		}
		
		
		
		
		
	}
}